package Z_durable;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @author： pang-yun
 * @date： 2021-12-15 10:02
 * 功能目的 ：用于 消息持久化
 */

public class MessageDurable {

    static final String MESSAGE_DURABLE = "MessageDurable";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setPassword("1234");
        connectionFactory.setUsername("pangyun");
        connectionFactory.setHost("127.0.0.1");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        // 队列持久化
        boolean durable = true;
        channel.queueDeclare(MESSAGE_DURABLE, durable, false, false, null);

        // 消息持久化
        String message = "durableMessage";
        channel.basicPublish("",MESSAGE_DURABLE, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes(StandardCharsets.UTF_8));

    }
}
